package com.lanhai.zhonglai.third;

import com.lanhai.zhonglai.beans.StationDataBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.StationDataTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.utils.Tools;

import java.util.Arrays;
import java.util.List;

/**
 * 缓存机,所有的缓存机都通用一个协议
 *
 * @author zhengwei
 */
public class HC implements IStationProtocol {


    @Override
    public byte[] agvArrived(byte line, TransDirectionEnum type) {
        byte[] buf = new byte[8];
        buf[1] = 1;
        switch (type) {
            case OnlyIn:
                buf[3] = 1;
                break;
            case OnlyOut:
                buf[3] = 0;
                break;
            default:
                break;
        }

        return buf;
    }

    @Override
    public byte[] applyLeave(byte line, TransDirectionEnum type) {
        byte[] buf = new byte[8];
        buf[1] = 2;
        return buf;
    }


    @Override
    public List<StationDataBean> analyseData(String ip, byte[] buf) {
        StationDataBean obj = new StationDataBean();
        obj.setLine(buf[0]);
        obj.setCmdStr(Tools.byte2HexStr(buf));
        obj.setIp(ip);
        if (buf[1] == 4) {
            //机台异常
            obj.setEventType(StationDataTypeEnum.Error);
            return Arrays.asList(obj);
        }
        if (buf[1] == 3) {
            //允许离开
            obj.setEventType(StationDataTypeEnum.AllowLeave);
            return Arrays.asList(obj);
        }
        if (buf[1] == 2) {
            //允许传输
            obj.setEventType(StationDataTypeEnum.AllowTrans);
            return Arrays.asList(obj);
        }
        if (buf[1] == 5) {
            //转动皮带
            obj.setEventType(StationDataTypeEnum.Receive);
            return Arrays.asList(obj);
        }
        obj.setEventType(StationDataTypeEnum.Info);
        StationMountBean mb = new StationMountBean();
        //缓存机用outNum代表机台数量，机台数量只有三种
        //0=空料  1=有部分料  x=满料，x为不同机台的满料值
        //数量是按传输次数算的，比如一车10个篮，算1
        mb.setOutNum(buf[3]);
        obj.setBean(mb);
        return Arrays.asList(obj);
    }

    @Override
    public byte[] agvLeave(byte line, TransDirectionEnum type) {
        byte[] buf = new byte[8];
        buf[1] = 0;
        return buf;
    }

    @Override
    public byte[] applyRecieve(byte line, TransDirectionEnum type) {
        byte[] buf = new byte[8];
        buf[1] = 3;
        switch (type) {
            case OnlyIn:
                buf[3] = 1;
                break;
            case OnlyOut:
                buf[3] = 0;
                break;
            default:
                break;
        }
        return buf;
    }

    @Override
    public byte[] transError(byte line, TransDirectionEnum type) {
        byte[] buf = new byte[8];
        buf[1] = 4;
        return buf;
    }
}
